package com.redrose.videodemo.utils;

import android.util.Log;


import com.redrose.videodemo.BuildConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志工具类
 */

public class LogUtils {

    private static final DateFormat LOGINFOFORMAT;
    private static final DateFormat SIMPLEFORMAT;
    private static final String TAG = "VideoDemo";
    private static final String fileName;
    private static final String filePath;
    private static final boolean DEBUG;

    static {
        filePath = PathUtil.getLogPath();
        File logDir = new File(filePath);
        if (!logDir.exists()) {
            logDir.mkdirs();
        }
        SIMPLEFORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        LOGINFOFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        fileName = "market_" + SIMPLEFORMAT.format(new Date()) + ".log";
        DEBUG = BuildConfig.DEBUG;
        Log.i(TAG, "file:" + filePath + fileName);
    }

    public static void d(String paramString) {
        d(TAG, paramString);
    }

    public static void d(String tag, String msg) {
        d(tag, msg, getPrefix());
    }

    private static void d(String tag, String msg, String extra) {
        if (DEBUG)
            Log.d(tag, extra + msg);
        writeFile(extra + tag + ":" + msg);
    }

    public static void e(String msg) {
        e(TAG, msg, getPrefix());
    }

    public static void e(String tag, String msg) {
        e(tag, msg, getPrefix());
    }

    private static void e(String tag, String msg, String extra) {
        if (DEBUG)
            Log.e(tag, extra + msg);
        writeFile(extra + tag + ":" + msg);
    }

    public static void e(String tag, String msg, Throwable ex) {
        if (DEBUG)
            Log.e(tag, msg, ex);
        try {
            if (makeDirs()) {
                PrintWriter localPrintWriter = new PrintWriter(new FileWriter(filePath + fileName, true));
                localPrintWriter.write("--------------------------error begin" + LOGINFOFORMAT.format(new Date()) + "-------------------------------");
                localPrintWriter.write("\n");
                ex.printStackTrace(localPrintWriter);
                localPrintWriter.write("\n");
                localPrintWriter.close();
            } else {
                Log.e(TAG, "创建文件失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void e(String paramString, Throwable paramThrowable) {
        e(paramString, "error", paramThrowable);
    }

    public static void e(Throwable paramThrowable) {
        e(TAG, "error", paramThrowable);
    }

    private static String getPrefix() {
        StackTraceElement localStackTraceElement = Thread.currentThread().getStackTrace()[4];
        String className = localStackTraceElement.getClassName();
        return "[" + Thread.currentThread().getName() + "][" +
                className.substring(1 + className.lastIndexOf("."))
                + "." + localStackTraceElement.getMethodName() +
                "(" + localStackTraceElement.getFileName() + ":" + localStackTraceElement.getLineNumber() + ")]";
    }

    public static void i(String msg) {
        i(TAG, msg, getPrefix());
    }

    public static void i(String tag, String msg) {
        i(tag, msg, getPrefix());
    }

    private static void i(String tag, String msg, String extra) {
        if (DEBUG) {
            Log.i(tag, extra + msg);
        }
        writeFile(extra + tag + ":" + msg);
    }

    private static boolean makeDirs() throws IOException {
        File dir = new File(filePath);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                return false;
            }
        }
        File logFile = new File(filePath, fileName);
        if (logFile.exists())
            return true;
        if (!logFile.createNewFile()) {
            Log.e(TAG, "creat file fail");
        } else {
            return true;
        }
        return false;
    }


    private static void writeFile(String msg) {
        try {
            if (makeDirs()) {
                FileWriter writer = new FileWriter(new File(filePath, fileName), true);
                writer.write(LOGINFOFORMAT.format(new Date()) + "\t");
                writer.write(msg + "\n");
                writer.flush();
                writer.close();
            } else {
                Log.e(TAG, "日志文件创建失败");
            }
        } catch (IOException e) {
            Log.e(TAG, "create file error", e);
        }

    }
}
